package com.example.ExampleProject2;

import android.app.*;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O, Marcin.M, Mateusz.K
 * Description: example andrid application
 * Date: 24.10.13
 * Time: 20:57
 */
public class HelloAndroidActivity extends Activity {
    private SharedPreferences preferences;
    public static final String MY_PREFERENCES = "myPreferences";

    private TextView music;
    private int counter = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SampleClass sampleClass = new SampleClass();
        sampleClass.method1();

        preferences = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
        String textFromPreferences = preferences.getString("MUSIC_KEY", "Muzyka off");
        music = (TextView) findViewById(R.id.music);
        music.setText(textFromPreferences);
    }

    public void finishApp(View view){
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putString("MUSIC_KEY", "Muzyka on");
        preferencesEditor.commit();
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(com.example.ExampleProject2.R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_help:
                openDialogHelp();
                return true;
            case R.id.action_settings:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /*
    * Event click button add notification
    * */
    public void addNotification(View v){
        if(v.getId() == R.id.addNotification){
            counter++;
            createNotification(counter, createTitle(), createContent(counter));
        }
    }

    /*
    * Method special created for unit
    * */
    public String createTitle(){
        return "UWAGA";
    }

    /*
    * Method special created for unit
    * */
    public String createContent(int counter){
        return "TO JEST POWIADOMIENIE NR: "+counter;
    }

    /*
    * Event click button remove notification
    * */
    public void clearNotification(View v){
        if(v.getId() == R.id.clearNotification){
            if (counter != 0){
                clearNotification(this.getApplicationContext(), counter);
                counter--;
            }
        }
    }

    /*
    * Create notification
    * */
    private void createNotification(int counter, String title, String content){
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(title)
                .setContentText(content);

        Intent resultIntent = new Intent(this, HelloAndroidActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(HelloAndroidActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(counter, mBuilder.build());
    }

    /*
    * Remove notification
    * */
    private void clearNotification(Context ctx, int notifyId){
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
        nMgr.cancel(notifyId);
    }

    /*
    * Dialog open with help
    * */
    public void openDialogHelp(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(R.string.title_help);
        alertDialogBuilder
                .setMessage(R.string.help_content)
                .setCancelable(false)
                .setPositiveButton(R.string.yes,new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        HelloAndroidActivity.this.finish();
                    }
                })
                .setNegativeButton(R.string.no,new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

}

